package frame;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;

import entity.Depot;
import entity.Student;

public class Visual extends JFrame implements ActionListener{
	private static final long serialVersionUID = 1L;
	public JComboBox<String> depotbox;
	private JTable jt;
	private DefaultTableModel dtm;
	private JLabel jl;
	private JTextField jtf[];
	private JButton jb[];
	public Control cl;
	
	private Integer index=0;
	public Integer getIndex() {
		return index;
	}

	public void setIndex(Integer index) {
		this.index = index;
	}

	public Visual() {
		cl= new Control();
		setSize(800, 600);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setTitle("Students&Schools");
		Dimension di = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((di.width - 800) / 2, (di.height - 600) / 2);
		setVisible(true);
		setLayout(null);
		
		depotbox=new JComboBox<String>(new String[]{"Computer","Math","Liberal Arts"});
		depotbox=new JComboBox<String>(cl.getdepotname());
		depotbox.setSize(200,50);
		depotbox.setLocation(50, 50);
		depotbox.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) {
				if(e.getStateChange()==ItemEvent.SELECTED){
					System.out.println("已监听");
					index=depotbox.getSelectedIndex();
					System.out.println(index);
					displayStudentInfo();
				}
				}
		});
		add(depotbox);
		
		jl=new JLabel("View All Students In Each Individual Department.");
		jl.setFont(new Font("Arial", Font.BOLD, 20));
		jl.setSize(600,50);
		jl.setLocation(300, 50);
		add(jl);
		
		JPanel jp=new JPanel();
		jp.setSize(600,300);
		jp.setLocation(100, 150);
		dtm=new DefaultTableModel(new Object[][]{}, new String[]{"Student ID", "Student Name","Student Address"});
		jt=new JTable(dtm);
		jt.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		JScrollPane jsp= new JScrollPane(jt);
		jsp.setPreferredSize(new Dimension(600, 300));
		jp.add(jsp);
		add(jp);
		
		JPanel jp2=new JPanel();
		jp2.setLayout(new GridLayout(1, 3));
		jp2.setLocation(100,480);
		jp2.setSize(new Dimension(350, 30));
		jtf=new JTextField[3];
		for(int i=0;i<jtf.length;i++){
			jtf[i]=new JTextField();
			jp2.add(jtf[i]);
		}
		add(jp2);
		
		JPanel jp3=new JPanel();
		jp3.setLayout(new GridLayout(1, 2));
		jp3.setLocation(470,480);
		jp3.setSize(new Dimension(200, 30));
		jb=new JButton[2];
		jb[0]=new JButton("ADD");
		jb[0].addActionListener(this);
		jp3.add(jb[0]);
		jb[1]=new JButton("DELETE");
		jb[1].addActionListener(this);
		jp3.add(jb[1]);
		add(jp3);
		displayStudentInfo();
		
	}

	public void displayStudentInfo(){
		dtm.setRowCount(0);
		List<Student> list=cl.getstulist(index);
		for (Student student : list) {
			dtm.addRow(new String[] {student.getSID().toString(),student.getSTUDENT_NAME(),student.getSTUDENT_ADDRESS()});
		}
	}
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new Visual();
			}
		});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String command=e.getActionCommand();
		if(command.equals("ADD")){
			Integer sid=Integer.parseInt(jtf[0].getText());
			String name=jtf[1].getText();
			String address=jtf[2].getText();
			Integer depotid=depotbox.getSelectedIndex()+1;
			String depotname=(String) depotbox.getSelectedItem();
			System.out.println(depotid);
			cl.insertStudent(sid, name, address, depotname, depotid);
			jtf[0].setText("");
			jtf[1].setText("");
			jtf[2].setText("");
			displayStudentInfo();
		}
		else if(command.equals("DELETE")){
			Integer sid=Integer.parseInt(jtf[0].getText());
			cl.deleteStudent(sid);
			jtf[0].setText("");
			jtf[1].setText("");
			jtf[2].setText("");
			displayStudentInfo();
		}
	}
}
